<template>
  <div id="app">
    <div style="padding: 20px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
        <el-form-item label="实验时间" prop="violationDate">
          <el-date-picker clearable
                          v-model="queryParams.startEndTime"
                          type="datetimerange"
                          range-separator="至"
                          value-format="yyyy-MM-dd hh:mm:ss"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实验名称" prop="courseName">
          <el-input
            v-model="queryParams.courseName"
            placeholder="请输入实验室名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="实验编号" prop="courseNo">
          <el-input
            v-model="queryParams.courseNo"
            placeholder="请输入实验室编号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="20">
        <el-col :span="6" v-for="(course, index) in courses" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>{{ course.courseName }}</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="handleReserve(course)">预约</el-button>
            </div>
            <div>
              <p>实验编号: {{ course.courseNo }}</p>
              <p>实验室: {{ course.labName }}</p>
              <p>实验地点: {{ course.location }}</p>
              <p>实验时间: {{ course.startTime }} - {{ course.endTime }}</p>
              <p>实验最大容纳人数: {{ course.maxExperimenters }}</p>
              <p>实验剩余容纳人数: {{ course.remainCapacity }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>


    <!-- 预约对话框 -->
    <el-dialog title="预约实验室" :visible.sync="dialogVisible" width="40%">
      <el-form :model="selectedCourse" ref="selectedLab" size="small" :inline="true" label-width="100px">
        <el-form-item label="实验室编号" prop="labNo">
          <el-input
            v-model="selectedCourse.courseNo"
            placeholder="请输入实验课程编号"
            disabled
          />
        </el-form-item>
        <el-form-item label="实验室名称" prop="courseName">
          <el-input
            v-model="selectedCourse.courseName"
            placeholder="请输入实验室名称"
            clearable
            disabled
          />
        </el-form-item>

        <el-form-item label="预约时间">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker> -
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmReserve">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {addLabBorrow} from "@/api/system/labBorrow";
import {listCourse} from "@/api/system/course";
import {addReservation} from "@/api/system/reservation";

export default {
  name: 'App',
  data() {
    return {
      courses: [],
      total: 0,
      queryParams:{

      },
      dialogVisible: false,
      selectedCourse: {
        startTime: null,
        endTime: null
      },
      form: {},
    }
  },
  methods: {
    handleReserve(course) {
      this.selectedCourse = course;
      if (this.selectedCourse.startTime) {
        this.form.startTime = this.selectedCourse.startTime
      }
      if (this.selectedCourse.endTime) {
        this.form.endTime = this.selectedCourse.endTime
      }
      this.dialogVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getCourseList(this.queryParams)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startEndTime = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getCourseList(param) {
      listCourse(param).then(response => {
        this.courses = response.rows;
        this.total = response.total;
      });
    },
    confirmReserve() {
      this.form.courseId = this.selectedCourse.id
      this.form.labId = this.selectedCourse.labId
      // 二次确认弹框
      this.$confirm('是否确认预约？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addReservation(this.form).then(response => {
          if (response.code === 200) {
            this.$message({
              message: `成功预约${this.selectedCourse.courseName},请等待管理员审批！`,
              type: 'success'
            });
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            });
          }
          this.dialogVisible = false;
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消预约'
        });
      });
    }
  },
  created() {
    this.getCourseList();
  }
}
</script>

<style>
.box-card {
  width: 100%;
  margin-bottom: 20px;
}
</style>
